﻿using System;
using System.Collections.Generic;
using System.Text;
using Slwy.Data.Entity.Interfaces;
using SqlSugar;
using Slwy.Logger.Contracts;
using System.Linq;

namespace Slwy.Data.Entity
{
    public class EditRepository<TEntity, TKey> : Repository<TEntity, TKey>, IEditRepository<TEntity, TKey>
        where TEntity : class, IEntityEdit<TKey>, new()
    {
        private readonly ILogger _logger;

        /// <summary>
        /// Initializes a new instance of the <see cref="Repository{TEntity,TKey}"/> class.
        /// </summary>
        /// <param name="logger">日志</param>
        public EditRepository(ILogger logger, IUnitOfWork unitOfWork)
            :base(logger, unitOfWork)
        {
            this._logger = logger;
        }

        public IUpdateable<TEntity> Recycle(IList<TKey> keys, string modifyUser)
        {
            return this.Client.Updateable<TEntity>()
                .UpdateColumns(m => new TEntity
                {
                    IsDelete = true,
                    ModifyUser = modifyUser
                })
                .Where(m => keys.Contains(m.KeyID));
        }

        public IUpdateable<TEntity> Recycle(TKey key, string modifyUser)
        {
            return this.Client.Updateable<TEntity>()
                .UpdateColumns(m => new TEntity
                {
                    IsDelete = true,
                    ModifyUser = modifyUser
                })
                .Where(m => key.Equals(m.KeyID));
        }

        public IUpdateable<TEntity> Recycle(TKey[] keys, string modifyUser)
        {
            return this.Client.Updateable<TEntity>()
                .UpdateColumns(m => new TEntity
                {
                    IsDelete = true,
                    ModifyUser = modifyUser
                })
                .Where(m =>  keys.Contains(m.KeyID));
        }
    }
}
